// 命令行输出处理
package pkgerrors

import (
	"fmt"
	"os"
	"strings"
	"time"
)

func (em *ExceptionManage) displayError(exc *PkgException) {
	var builder strings.Builder
	//31红色，32绿色，33黄色，34蓝色，35紫色，36青色，37白色
	// 1;31 红色粗体
	switch exc.OutputType {
	case OutputTypeSuccess:
		// 成功
		builder.WriteString(em.colorize("✓ ", "32")) // 绿色
		builder.WriteString(em.colorize(exc.Message, "32"))
		fmt.Fprint(os.Stderr, builder.String())
		os.Exit(0) // 成功后退出
	case OutputTypeInfo:
		// 提示信息
		builder.WriteString(em.colorize("[info] ", "36")) // 青色
		builder.WriteString(em.colorize(exc.Message, "36"))
	case OutputTypeWarn:
		// 警告信息
		builder.WriteString(em.colorize("⚠ ", "33")) // 黄色
		builder.WriteString(em.colorize(exc.Message, "33"))
	case OutputTypeError:
		// 错误信息
		builder.WriteString(em.colorize("✗ ", "31")) // 红色
		builder.WriteString(em.colorize(exc.Message, "31"))
	case OutputTypeFatal:
		// 致命错误信息
		builder.WriteString(em.colorize("✗ FATAL:  ", "31")) // 红色
		builder.WriteString(em.colorize(exc.Message, "31"))
	case OutputTypeDebug:
		// 调试信息
		builder.WriteString(em.colorize("DEBUG: ", "35")) // 紫色
		builder.WriteString(em.colorize(exc.Message, "35"))

		/* default:
		// 默认
		builder.WriteString(em.formatWithColor(colorRed, "错误：%s\n", exc.Message)) */
	}

	/* // 上下文信息
	if exc.Code > 0 {
		builder.WriteString(fmt.Sprintf("Error Code: %s\n", em.colorize(fmt.Sprintf("%04d", exc.Code), "36")))
	}

	if exc.Package != "" {
		builder.WriteString(fmt.Sprintf("Package: %s\n", em.colorize(exc.Package, "33")))
	}

	// 如果有版本信息
	if exc.Version != "" {
		builder.WriteString(fmt.Sprintf("Version: %s\n", em.colorize(exc.Version, "33")))
	} */

	// 解决方案
	/* if solutions := m.getSolutions(err); solutions != "" {
		builder.WriteString("\n" + solutions + "\n")
	} */

	// 调试信息
	if em.verbose {

		if exc.Inner != nil {
			builder.WriteString(fmt.Sprintf("Caused by: %v\n", exc.Inner))
		}
		// builder.WriteString(m.getDebugInfo(err))
	}

	fmt.Fprint(os.Stderr, builder.String())
}

func (em *ExceptionManage) colorize(text, ansiCode string) string {
	if !em.colorEnabled {
		return text
	}
	return fmt.Sprintf("\033[%sm%s\033[0m", ansiCode, text)
}

// 获取解决方案
func (em *ExceptionManage) getSolutions(err *PkgException) string {
	solutions := map[int][]string{
		1001: { // Package not found
			"• Check the package name for typos",
			"• Search for packages: " + em.colorize("pkg search <name>", "32"),
		},
		2001: { // Network error
			"• Check your internet connection",
			"• Retry with: " + em.colorize("pkg retry", "32"),
			"• Configure mirror: " + em.colorize("pkg config set mirror <url>", "32"),
		},
		3001: { // Dependency conflict
			"• Try updating: " + em.colorize("pkg update", "32"),
			"• Resolve conflicts: " + em.colorize("pkg resolve", "32"),
		},
	}

	if sugg, ok := solutions[err.Code]; ok {
		builder := strings.Builder{}
		builder.WriteString(em.colorize("Suggested solutions:", "1;37"))
		for _, s := range sugg {
			builder.WriteString("\n" + s)
		}
		return builder.String()
	}

	return em.colorize("Run with --verbose for more details", "2;37")
}

// 调试信息
func (em *ExceptionManage) getDebugInfo(err *PkgException) string {
	builder := strings.Builder{}
	builder.WriteString(em.colorize("\nDebug Information:\n", "1;34"))
	builder.WriteString(fmt.Sprintf("Timestamp: %s\n", err.Timestamp.Format(time.RFC3339)))
	builder.WriteString(fmt.Sprintf("Category: %s\n", err.Category))

	/* if len(err.Stack) > 0 {
		builder.WriteString("\nStack Trace:\n")
		builder.Write(err.Stack)
	} */

	if len(err.Metadata) > 0 {
		builder.WriteString("\nMetadata:\n")
		for k, v := range err.Metadata {
			builder.WriteString(fmt.Sprintf("  %s: %v\n", k, v))
		}
	}

	return builder.String()
}
